package com.tax.common.util;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class HashUtil {
	private static Logger log = LogManager.getLogger(HashUtil.class);
	private final static String str = "abcdefghigklmnopkrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";

	/* Hashing the string */
	public final static String strHash_MD5(String str) {

		return hashAlgorithm(str, "md5");
	}

	/* generate a random id */
	public final static String getRandomId() {
		return hashAlgorithm(genRandomStr(32), "md5");
	}

	public final static String getExchangeCodeRandomId() {
		return hashAlgorithm(genRandomStr(8), "md5");
	}

	private static String genRandomStr(int length) {
		Random r = new Random();
		StringBuffer sf = new StringBuffer();

		for (int i = 0; i < length; i++) {
			int number = r.nextInt(62); // 0~61
			sf.append(str.charAt(number));
		}
		sf.append(System.currentTimeMillis()); // add current time
		return sf.toString();
	}

	/* here algorithm are SHA, SHA-256, md5 */
	private static String hashAlgorithm(String str, String Algorithm) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(Algorithm);
		} catch (NoSuchAlgorithmException e) {
			log.debug("error using Hash method");
			log.error(ExceptionUtils.getStackTrace(e));
		}
		if (md == null) {
			return null;
		}
		md.update(str.getBytes()); // first we must get the bytes of string
		byte[] bs = md.digest(); // begin encryption

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bs.length; i++) { // bytes convert string
			int v = bs[i] & 0xff;
			if (v < 16) {
				sb.append(0);
			}
			sb.append(Integer.toHexString(v));
		}
		return sb.toString();
	} // hashAlgorithm

	public static String getRandomNumStr(int length) {
		Random random = new Random();
		int i;
		StringBuilder sb = new StringBuilder();
		while (true) {
			int next = random.nextInt();
			// i = Math.abs(random.nextInt());
			i = Math.abs(next);
			if (i == 0) {
				continue;
			}
			sb.append(Integer.toString(i));
			if (sb.length() > length) {
				break;
			}
		} // while

		return sb.substring(0, length);

	}

	public static String getOrderId() {
		String sdf = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		int hashCodeV = UUID.randomUUID().toString().hashCode();
		if (hashCodeV < 0) {// 有可能是负数
			hashCodeV = -hashCodeV;
		}
		String hcv = hashCodeV + "";
		String rad = "";
		if (hcv.length() < 10) {
			int lle = Math.abs(10 - hcv.length());
			System.out.println(lle);
			for (int i = 0; i < lle; i++) {
				SecureRandom secureRandom = new SecureRandom();
				rad += secureRandom.nextInt(10) + "";
			}
		} else {
			hcv = hcv.substring(0, 10);
		}
		String id = sdf + rad + hcv;
		return id;
	}


	public static String getId() {
		String sdf = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
		int hashCodeV = UUID.randomUUID().toString().hashCode();

		if (hashCodeV < 0) {// 有可能是负数
			hashCodeV = -hashCodeV;
		}

		String hcv = hashCodeV + "";
        System.out.println(hashCodeV);
		String rad = "";
		if (hcv.length() < 5) {
			int lle = Math.abs(5 - hcv.length());
			for (int i = 0; i < lle; i++) {
				SecureRandom secureRandom = new SecureRandom();
				rad += secureRandom.nextInt(10) + "";
			}
		} else {

			hcv = hcv.substring(0, 5);
		}
        System.out.println(rad);
		String id = sdf + rad + hcv;
        System.out.println(id);
		return id;
	}

    public static void main(String[] args) {
        String id = HashUtil.getId();
        System.out.println(id.length());
    }
}
